﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Mes.Pendant.Common;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.PermissionService;
using Mes.Pendant.PermissionService.Dto;
using Mes.Pendant.RBACFiles;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Mes.Pendant.PermissionServicess
{
    public class PermissionServices:ApplicationService,IPermissionServices
    {
        private readonly IRepository<Permission,int> permissionsRepository;

        public PermissionServices(IRepository<Permission, int> permissionsRepository)
        {
            this.permissionsRepository = permissionsRepository;
        }

        /// <summary>
        /// 获取权限数据
        /// </summary>
        /// <param name="permissionName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetPermissionList(string? permissionName,int index=1,int limit=10)
        {
            var predicate=PredicateBuilder.New<Permission>(true);
            if (!string.IsNullOrEmpty(permissionName))
            {
                predicate=predicate.And(m=>m.PermissionName.Contains(permissionName));
            }
            var permissionList=await  permissionsRepository.GetListAsync(predicate);
            PagedResult pagedResult= new PagedResult();
            pagedResult.PageSize=limit;
            pagedResult.CurrentPage=index;
            pagedResult.RowCount = permissionList.Count();
            pagedResult.Queryable=permissionList.OrderBy(x=>x.Id).Skip((index-1)*limit).Take(limit).AsQueryable();
            pagedResult.Queryable=pagedResult.Queryable.MapToList<PermissionListDto>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 添加权限数据
        /// </summary>
        /// <param name="addPermissiondto"></param>
        /// <returns></returns>
        public async Task<int> AddPermissions(AddPermissiondto addPermissiondto)
        {
            var permissiondto= addPermissiondto.MapTo<Permission>();
            await permissionsRepository.InsertAsync(permissiondto);
            return 1;
        }

        /// <summary>
        /// 通过id获取权限数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PermissionListDto> SelectByidPerm(int id)
        {
            var perlistdto=permissionsRepository.GetAsync(id).Result;
            var perdto = perlistdto.MapTo<PermissionListDto>();
            return perdto;
        }

        /// <summary>
        /// 修改权限数据
        /// </summary>
        /// <param name="updatePermissiondto"></param>
        /// <returns></returns>
        public async Task<int> UpdatePermissions(AddPermissiondto updatePermissiondto)
        {
            Permission permission =updatePermissiondto.MapTo<Permission>();
            await permissionsRepository.UpdateAsync(permission);
            return 1;
        }

        /// <summary>
        /// 启用停用按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> EditStates(int id)
        {
            var perdto=permissionsRepository.GetAsync(id).Result;
            if (perdto.PermissionStatus == 1)
            {
                perdto.PermissionStatus = 2;
            }
            else
            {
                perdto.PermissionStatus = 1;
            }
            await permissionsRepository.UpdateAsync(perdto);
            return 1;
        }
    }
}
